Image
Maintainers: @HeroicKatora, @fintelia
An Image Processing Library
This crate provides basic image processing functions and methods for converting to and from various image formats.
All image processing functions provided operate on types that implement the GenericImageView
and GenericImage
traits and return an ImageBuffer
.
Supported Image Formats
image
provides implementations of common image format encoders and decoders.
Format | Decoding | Encoding |
---|---|---|
AVIF | Only 8-bit ** | Lossy |
BMP | Yes | Rgb8, Rgba8, Gray8, GrayA8 |
DDS | DXT1, DXT3, DXT5 | No |
Farbfeld | Yes | Yes |
GIF | Yes | Yes |
HDR | Yes | Yes |
ICO | Yes | Yes |
JPEG | Baseline and progressive | Baseline JPEG |
OpenEXR | Rgb32F, Rgba32F (no dwa compression) | Rgb32F, Rgba32F (no dwa compression) |
PNG | All supported color types | Same as decoding |
PNM | PBM, PGM, PPM, standard PAM | Yes |
QOI | Yes | Yes |
TGA | Yes | Rgb8, Rgba8, Bgr8, Bgra8, Gray8, GrayA8 |
TIFF | Baseline(no fax support) + LZW + PackBits | Rgb8, Rgba8, Gray8 |
WebP | Yes | Rgb8, Rgba8 * |
- * Requires the
webp-encoder
feature, uses the libwebp C library. - ** Requires the
avif-decoder
feature, uses the libdav1d C library.
The ImageDecoder
and ImageDecoderRect
Traits
All image format decoders implement the ImageDecoder
trait which provide
basic methods for getting image metadata and decoding images. Some formats
additionally provide ImageDecoderRect
implementations which allow for
decoding only part of an image at once.
The most important methods for decoders are...
- dimensions: Return a tuple containing the width and height of the image.
- color_type: Return the color type of the image data produced by this decoder.
- read_image: Decode the entire image into a slice of bytes.
Pixels
image
provides the following pixel types:
- Rgb: RGB pixel
- Rgba: RGB with alpha (RGBA pixel)
- Luma: Grayscale pixel
- LumaA: Grayscale with alpha
All pixels are parameterised by their component type.
Images
Individual pixels within images are indexed with (0,0) at the top left corner.
The GenericImageView
and GenericImage
Traits
Traits that provide methods for inspecting (GenericImageView
) and manipulating (GenericImage
) images, parameterised over the image's pixel type.
Some of these methods for GenericImageView
are...
- dimensions: Return a tuple containing the width and height of the image.
- get_pixel: Returns the pixel located at (x, y).
- pixels: Returns an Iterator over the pixels of this image.
While some of the methods for GenericImage
are...
- put_pixel: Put a pixel at location (x, y).
- copy_from: Copies all of the pixels from another image into this image.
Representation of Images
image
provides two main ways of representing image data:
ImageBuffer
An image parameterised by its Pixel types, represented by a width and height and a vector of pixels. It provides direct access to its pixels and implements the GenericImageView
and GenericImage
traits.
use ;
// Construct a new RGB ImageBuffer with the specified width and height.
let img: RgbImage = new;
// Construct a new by repeated calls to the supplied closure.
let mut img = from_fn;
// Obtain the image's width and height.
let = img.dimensions;
// Access the pixel at coordinate (100, 100).
let pixel = img;
// Or use the `get_pixel` method from the `GenericImage` trait.
let pixel = *img.get_pixel;
// Put a pixel at coordinate (100, 100).
img.put_pixel;
// Iterate over all pixels in the image.
for pixel in img.pixels
DynamicImage
A DynamicImage
is an enumeration over all supported ImageBuffer<P>
types.
Its exact image type is determined at runtime. It is the type returned when opening an image.
For convenience DynamicImage
reimplements all image processing functions.
DynamicImage
implement the GenericImageView
and GenericImage
traits for RGBA pixels.
SubImage
A view into another image, delimited by the coordinates of a rectangle. The coordinates given set the position of the top left corner of the rectangle. This is used to perform image processing functions on a subregion of an image.
use ;
let mut img: RgbImage = new;
let subimg = crop;
assert!;
Image Processing Functions
These are the functions defined in the imageops
module. All functions operate on types that implement the GenericImage
trait.
Note that some of the functions are very slow in debug mode. Make sure to use release mode if you experience any performance issues.
- blur: Performs a Gaussian blur on the supplied image.
- brighten: Brighten the supplied image.
- huerotate: Hue rotate the supplied image by degrees.
- contrast: Adjust the contrast of the supplied image.
- crop: Return a mutable view into an image.
- filter3x3: Perform a 3x3 box filter on the supplied image.
- flip_horizontal: Flip an image horizontally.
- flip_vertical: Flip an image vertically.
- grayscale: Convert the supplied image to grayscale.
- invert: Invert each pixel within the supplied image This function operates in place.
- resize: Resize the supplied image to the specified dimensions.
- rotate180: Rotate an image 180 degrees clockwise.
- rotate270: Rotate an image 270 degrees clockwise.
- rotate90: Rotate an image 90 degrees clockwise.
- unsharpen: Performs an unsharpen mask on the supplied image.
For more options, see the imageproc
crate.
Examples
Opening and Saving Images
image
provides the open
function for opening images from a path. The image
format is determined from the path's file extension. An io
module provides a
reader which offer some more control.
use GenericImageView;
Generating Fractals
//! An example of generating julia fractals.
Example output:
Writing raw buffers
If the high level interface is not needed because the image was obtained by other means, image
provides the function save_buffer
to save a buffer to a file.